<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>采购图书清单</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>
    <link rel="stylesheet" href="./css/common.css">
</head>

<body>
    <header>
        <div class="wel" style="margin-top:10px;">
            <h3>欢迎来到强哥图书管理系统</h3>
        </div>
        <nav>
            <ul id="nav-list" style=" margin-top:19px;"></ul>
        </nav>
    </header>
    <div class="bookContainer">
        <h2>采购图书清单</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>商家</td>
                    <td>总价</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody>
                <!-- <tr>
                    <td><input type="checkbox" name="selectBook" value="1" id="selectBook" class="book-select"></td>
                    <td>1</td>
                    <td>大秦帝国第一册</td>

                    <td>23</td>
                    <td>33.00</td>
                    <td>北京出版社</td>
                    <td>可借阅</td>
                    <td>

                        <div class="op">
                            <a href="book_update.html?bookId=1">修改</a>
                            <a href="javascript:void(0)" onclick="deleteBook()">删除</a>
                        </div>
                    </td>
                </tr> -->


            </tbody>
        </table>

        <!-- <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div> -->
        <script>
const storedData = JSON.parse(localStorage.getItem('loginData')) || {};
console.log("storedData",storedData);
        const userRole = storedData || 'guest'; // 假设用户数据中有一个'role'字段
 console.log("userRole",userRole);
        // 获取导航列表的DOM元素
        const navList = document.getElementById('nav-list');
 
        // 创建一个函数来添加导航项
        function addNavItem(text, href) {
            const li = document.createElement('li');
            const a = document.createElement('a');
            a.href = href;
            a.textContent = text;
            li.appendChild(a);
            navList.appendChild(li);
        }
 
        // 根据用户角色添加相应的导航项
        if (userRole === 'admin') {
            addNavItem('登录', 'login.html');
            addNavItem('注册', 'register.html');
            addNavItem('图书列表', 'book_list.html');
            addNavItem('归还图书', 'return_list.html');
            addNavItem('采购界面', 'purchase.html');
            addNavItem('采购清单', 'addbook_list.html');
           
            addNavItem('个人中心', 'personal_admin.html');
            // addNavItem('c', 'book_list.html');
            // addNavItem('图书列表', 'book_list.html');
            // ... 其他管理员可见的链接
        } else if (userRole === 'user') {
            addNavItem('登录', 'login.html');
            addNavItem('注册', 'register.html');
            addNavItem('图书列表', 'book_list_B.html');       
            addNavItem('个人中心', 'personal.html');
            // ... 其他用户可见的链接
        } else {
            // 对于未登录或未知角色的用户，可能只显示登录链接
            addNavItem('登录', 'login.html');

        }



            selectAddBook();
            function selectAddBook() {
                $.ajax({
                    url: "/buy/selectAddBook?buyerName=zhangsan",
                    type: "get",
                    // success:function(result) {
                    success: function (body) {
                        if (body.code == "NOLOGIN") {
                            location.href = "login.html";
                        }
                        var result = body;

                        console.log("Response body:", body);

                        if (!result) {
                            alert("无数据或数据格式不正确");
                            return;
                        }
                        console.log("Data:", result);
                        console.log(typeof buyBooks);  // 查看是否为 'object'
                        console.log(Array.isArray(buyBooks));  // 确认是否为数组

                        var buyBooks = result;
                        console.log('buyBooks',buyBooks);
                        if (buyBooks == null) {
                            return;
                        }
                        var finalHTML = "";
                        console.log("qqq");
                        for (var book of buyBooks) {
                            finalHTML += '<tr>';
                            finalHTML += '<td><input type="checkbox" name="selectBook" value="' + book.id + '" id="selectBook" class="book-select"></td>';
                            finalHTML += '<td>' + book.id + '</td>';
                            finalHTML += '<td>' + book.bookName + '</td>';
                            finalHTML += ' <td>' + book.count + '</td>';

                            finalHTML += '<td>' + book.price + '</td>';
                            finalHTML += ' <td>' + book.merchant + '</td>';
                            finalHTML += ' <td>' + book.price * book.count + '</td>';
                            finalHTML += ' <td>';
                            finalHTML += '  <div class="op">';
                            finalHTML += ' <a href="book_update.html?bookId=' + book.id + '">修改</a>';
                            finalHTML += '  <a href="javascript:void(0)" onclick="deleteBook(' + book.id + ')">删除</a>';
                            finalHTML += ' </div></td></tr>';
                        }

                        $("tbody").html(finalHTML);
                    },
                    error: function (error) {
                        if (error.status == 401) {
                            // alert("用户未登录");
                            location.href = "login.html"
                        }
                        //    console.log(error);
                    }
                });
            }


            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    //删除图书
                    alert("id", id);
                    $.ajax({
                        url: "/book/deleteBook",
                        type: "post",
                        data: {
                            id: id
                        },
                        success: function (result) {
                            console.log("删除结果：" + result);
                            if (result) {
                                alert("删除成功");
                                location.href = "book_list.html";
                            } else {
                                alert("删除失败");
                            }
                        }
                    });
                }
            }
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    //获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectBook']:checked").each(function () {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    //alert("批量删除成功");
                    $.ajax({
                        url: "/book/batchDelete",
                        type: "post",
                        data: {
                            ids: ids
                        },
                        success: function (result) {
                            console.log(result.data);
                            console.log("isDelete" + result);
                            if (result != null && result.data == true) {

                                // if(result) {
                                alert("批量删除成功");
                                location.href = "book_list.html";
                            } else {
                                alert("删除失败");
                            }
                        }
                    });
                }
            }

        </script>
    </div>
</body>

</html>